package com.atguigu.gulimall.cart.Controller;

import com.atguigu.gulimall.cart.Interceptor.CartInterceptor;
import com.atguigu.gulimall.cart.To.UserInfoTo;
import com.atguigu.gulimall.cart.service.CartService;
import com.atguigu.gulimall.cart.vo.CartItemVo;
import com.atguigu.gulimall.cart.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

      @Autowired
      CartService cartService;

    /**
     * 获取购物车 所有信息
     * @return
     */
     @GetMapping("/cart.html")
    public String cartPageList(Model model) throws ExecutionException, InterruptedException {
         CartVo cart=cartService.getAllCartItem();
        model.addAttribute("cart",cart);
        return "cartList";
    }


    /**
     *  添加购物车
     */
    @GetMapping("/addCartItem")
    public String addCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num
            ,  RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
            CartItemVo cartItemVo= cartService.addToCart(skuId,num);
            redirectAttributes.addAttribute("skuId",skuId);
            return "redirect:http://cart.gulimall.com/addCartSuccess.html";
    }

    /**
     * 防止表单重复提交   让添加 购物车功能跳转 到此功能
     * 当 重定向 后 地址发生变化  重复刷新 就去
     * @return
     */
    @GetMapping("/addCartSuccess.html")
    public String addCartSuccess(@RequestParam("skuId") Long skuId,Model model){
        CartItemVo cartItemVo=cartService.getCart(skuId);
        model.addAttribute("cartItem",cartItemVo);
        return "success";
    }



    @GetMapping("/getItemToOrder")
    @ResponseBody
    public List<CartItemVo> getItemsToOrderConfirm(){
        return cartService.getItemsToOrderConfirm();
    }







}
